<?php
function ajax()
{
	global $dsql;
	$login_row = $dsql->GetOne("SELECT login_type FROM yiqiu_login_set");
	$login_type = unserialize($login_row['login_type']);
	$html = '<div><ul>';
	if(in_array('qq',$login_type))
	{
		$html .= "<li><a href='yiqiu_login_app/api/qq'><img src='yiqiu_login_app/statics/images/QQ.png'></a></li>";
	}
	
	if(in_array('weixin',$login_type)){
		$html .= "<li><a href='yiqiu_login_app/api/weixin'><img src='yiqiu_login_app/statics/images/weixin.png'></a></li>";
	}
	
	if(in_array('weibo',$login_type)){
		$html .= "<li><a href='yiqiu_login_app/api/weibo'><img src='yiqiu_login_app/statics/images/weibo.png'></a></li>";
	}
	$html .= '</ul></div>';
	return $html;
}

function D()
{
	global $cfg_basehost,$cfg_soft_lang;
	$file_path = YIQIUDATA."/code.txt";
	$content = file_get_contents($file_path);
	$keyRow = explode("|",$content);
	$html = 2;
	if(preg_match("#{$keyRow[0]}#",$cfg_basehost))
	{
		$parmurl = "http://api.yiqiu.org";
		$httpdown = new DedeHttpDown();
		$httpdown->OpenUrl($parmurl."/?ac=check&key=".$content."&url=".$_SERVER['HTTP_HOST']);
		$html = $httpdown->GetHtml();
		if($cfg_soft_lang=='gb2312') $html = utf82gb($html);
	}
	
	if($html == 2)
	{
		unlink(YIQIUDATA."/code.txt");
	}
	
	return $html;
}

function C()
{
	$result = D();
	if($result == 2)
	{
		die("Your site is not authorized!QQ:3627256");
	}
}

function L($login_type)
{
	global $dsql;
	$login_row = $dsql->GetOne("SELECT * FROM yiqiu_login_set");

	$login_infos = unserialize($login_row['login_infos']);
	if($login_type == 'qq')
	{
		$qc = new QC();
		$qc->qq_login();
	}else if($login_type == 'weixin'){
		$callback = $login_infos['weixin']['callback'];
		$AppID = $login_infos['weixin']['appid'];

		$state = md5(uniqid(rand(), TRUE));
		$_SESSION["wx_state"] = $state;
		$callback = urlencode($callback);

		//$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid={$AppID}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";

        $wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$AppID}&redirect_uri={$callback}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
//        var_dump($wxurl);exit;
		header("Location: $wxurl");
	}else if($login_type == 'weibo'){
		$sina=new sinaPHP($login_infos['weibo']['appkey'], $login_infos['weibo']['appsecret']);
		$login_url=$sina->login_url($login_infos['weibo']['callback']);
		header('location:'.$login_url);
	}
}

function fopen_url($url) 
{ 
    if (function_exists('file_get_contents')) { 
        $file_content = @file_get_contents($url); 
    } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){ 
        $i = 0; 
        while (!feof($file) && $i++ < 1000) { 
            $file_content .= strtolower(fread($file, 4096)); 
        } 
        fclose($file); 
    } elseif (function_exists('curl_init')) { 
        $curl_handle = curl_init(); 
        curl_setopt($curl_handle, CURLOPT_URL, $url); 
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 
        curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); 
        curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); //寮曠敤鍨冨溇閭浠舵鏌
        $file_content = curl_exec($curl_handle); 
        curl_close($curl_handle); 
    } else { 
        $file_content = ''; 
    } 
    return $file_content; 
}

function update_sql($sqlquery)
{
	global $dsql;
	$sqlquery = str_replace("\r","",$sqlquery);
	$sqls = preg_split("#;[ \t]{0,}\n#",$sqlquery);
	$nerrCode = ""; $i=0;
	foreach($sqls as $q)
	{
		$q = trim($q);
		if($q=="")
		{
			continue;
		}
		//echo $q;exit;
		$dsql->ExecuteNoneQuery($q);
		$errCode = trim($dsql->GetError());
		if($errCode=="")
		{
			$i++;
		}
		else
		{
			file_put_contents(YIQIUDATA."/error.txt",$errCode,FILE_APPEND);
		}
	}
	
}